package com.lk.user.controller;

import com.lk.user.dto.AccountLoginDTO;
import com.lk.user.dto.AccountRegisterDTO;
import com.lk.user.dto.GmmLoginDTO;
import com.lk.user.dto.YzmLoginDTO;
import com.lk.user.service.ILogininfoService;
import com.lk.user.domain.Logininfo;
import com.lk.user.query.LogininfoQuery;
import com.lk.basic.util.AjaxResult;
import com.lk.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/logininfo")
public class LogininfoController {
    @Autowired
    public ILogininfoService logininfoService;


    /**
     * 保存和修改公用的
     * @param logininfo  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Logininfo logininfo){
        try {
            if( logininfo.getId()!=null)
                logininfoService.update(logininfo);
            else
                logininfoService.insert(logininfo);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delById(@PathVariable("id") Long id){
        try {
            logininfoService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selById(@PathVariable("id")Long id)
    {
        try {
            Logininfo logininfo = logininfoService.selById(id);
            return AjaxResult.me().setResult(logininfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult selAll(){

        try {
            List< Logininfo> list = logininfoService.selAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult pageList(@RequestBody LogininfoQuery query)
    {
        try {
            PageList<Logininfo> pageList = logininfoService.queryPage(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody AccountLoginDTO dto)
    {
        try {
            return logininfoService.accountLogin(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }
    @PostMapping("/wechat/login")
    public AjaxResult wechatLogin(@RequestBody Map<String,Object> map)
    {
        try {
            return logininfoService.wechatLogin(map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("微信登录失败！"+e.getMessage());
        }
    }
    @PostMapping("/yzmlogin")
    public AjaxResult yzmLogin(@RequestBody YzmLoginDTO dto)
    {
        try {
            return logininfoService.yzmLogin(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }
    @PostMapping("/gmmlogin")
    public AjaxResult gmmLogin(@RequestBody GmmLoginDTO dto)
    {
        try {
            return logininfoService.gmmLogin(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }
}
